from flask import Blueprint,request,jsonify
from functools import wraps
class MyBlueprint(Blueprint):
    def __init__(self, name, import_name):
        super().__init__(name, import_name)
    def GET(self,url):
        def wrapper(func):
            @self.route(url,methods=["GET"])
            @wraps(func)
            def _wrapper():
                argcount=func.__code__.co_argcount
                varnames=func.__code__.co_varnames #会包含返回值
                kargs={}
                for i in range(argcount):
                    var=varnames[i]
                    val=request.values.get(var)
                    if val==None:
                        val=request.json.get(var)
                    kargs[var]=val
                return jsonify(func(**kargs))
            return _wrapper
        return wrapper
    def POST(self,url):
        def wrapper(func):
            @self.route(url,methods=["POST"])
            @wraps(func)
            def _wrapper():
                argcount=func.__code__.co_argcount
                varnames=func.__code__.co_varnames #会包含返回值
                kargs={}
                for i in range(argcount):
                    var=varnames[i]
                    val=request.values.get(var)
                    if val==None:
                        val=request.json.get(var)
                    kargs[var]=val
                return jsonify(func(**kargs))
            return _wrapper
        return wrapper
    def RequestMapping(self,url,methods=["GET","POST"]):
        def wrapper(func):
            @self.route(url,methods=methods)
            @wraps(func)
            def _wrapper():
                argcount=func.__code__.co_argcount
                varnames=func.__code__.co_varnames #会包含返回值
                kargs={}
                for i in range(argcount):
                    var=varnames[i]
                    val=request.values.get(var)
                    if val==None:
                        val=request.json.get(var)
                    kargs[var]=val
                return jsonify(func(**kargs))
            return _wrapper
        return wrapper